<template>
<div class="ratingselect" >
  <div class="rating-type">
    <span class="all" @click="select(2,$event)" :class="{'active':selectType===2}" v-on:click="changeType(2)">{{desc.all}}<label>{{ratings.length}}</label></span>
    <span class="recommand" :class="{'active':selectType===0}" @click="select(0,$event)" v-on:click="changeType(0)">{{desc.positive}} <label>{{positives.length}}</label></span>
    <span class="no-recommand" :class="{'active':selectType===1}" @click="select(1,$event)" v-on:click="changeType(1)">{{desc.neigative}} <label>{{negatives.length}}</label></span>
  </div>
  <div class="switch"><span class="icon off" :class="{'on':onlyContent===false}" v-on:click="changeSwitch(onlyContent)"></span>只看内容的评价</div>
</div>
</template>

<script type="text/ecmascript-6">
  const  POSITIVE = 0;
  const NEGATIVE =1;
  const ALL =2;
export default{
  props: {
    ratings:{
      type:Array,
      default(){
        return [];
      }
    },
    desc: {
      type:Object,
      default(){
        return{
          all:'全部',
          positive:'满意',
          neigative:'不满意'
        }
      }
    }
  },
  data(){
    return{
      selectType:ALL,
      onlyContent:true
    }
  },
  methods:{
    select(type,event){
      if(!event._constructed){
        return;
      }
      this.selectType=type;
    },
    changeType(type){
      if(!event._constructed){
        return;
      }
      this.$emit('ratingType',type);
    },
    changeSwitch(status){
      if(!event._constructed){
        return;
      }
     if(this.onlyContent === status){
       this.onlyContent=!status
     }else{
       this.onlyContent=status
     }
      this.$emit('changeSwitchP',status);
    }
  },
  computed:{
    positives(){
      return this.ratings.filter((rating) => {
        return rating.rateType === POSITIVE;
      })
    },
    negatives(){
      return this.ratings.filter((rating) => {
        return rating.rateType === NEGATIVE;
      })
    }
  }
}
</script>

<style>
  .rating-type span{
    display: inline-block;
    font-size: 12px;
    color: #fff;
    width: 60px;
    height: 32px;
    line-height:32px;
    text-align:center;
    border-radius: 3px;
    margin-right:20px;
  }
  .rating-type .all {
    background-color: #ccecf8;
    color: #7e8c8d;
  }
  .rating-type .all.active{
    background-color: #00a0dc;
    color: #fff;
  }
  .rating-type .recommand{
    background-color: #ccecf8;
    color: #7e8c8d;
  }
  .rating-type .no-recommand{
    background-color: #e9ebec;
    color: #7e8c8d;
  }
  .rating-type .recommand.active{
    background-color: #00a0dc;
    color: #fff;
  }
  .rating-type .no-recommand.active{
    background-color: #93999f;
    color: #fff;
  }
  .switch{
    padding:15px 0;
    width: 100%;
    height: 25px;
    line-height:25px;
  }
  .switch span {
    margin:0;
    float: left;
    line-height:25px;
    display: inline-block;
    width: 30px;
    height: 100%;
    background:url(off.png) no-repeat;
    background-size:30px;
  }
  .switch span.on{
    background:url(on.png) no-repeat;
    background-size:30px;
  }

</style>
